package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.constants.GatewayTaskTypeConstant;
import cn.stylefeng.guns.modular.gridsystem.entity.GatewayFirmware;
import cn.stylefeng.guns.modular.gridsystem.enums.GatewayFirmwareExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.mapper.GatewayFirmwareMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.GatewayFirmwareParam;
import cn.stylefeng.guns.modular.gridsystem.service.GatewayFirmwareService;
import cn.stylefeng.guns.modular.gridsystem.service.GatewayTaskService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网关固件表 服务实现类
 *
 * @author zxw
 * @date 2022/11/03 11:41
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class GatewayFirmwareServiceImpl extends ServiceImpl<GatewayFirmwareMapper, GatewayFirmware> implements GatewayFirmwareService {

    private final GatewayTaskService taskService;

    @Override
    public PageResult<GatewayFirmware> page(GatewayFirmwareParam param) {

        // 构造条件
        LambdaQueryWrapper<GatewayFirmware> queryWrapper = new LambdaQueryWrapper<GatewayFirmware>()
                .eq(ObjectUtil.isNotEmpty(param.getStationId()),GatewayFirmware::getStationId, param.getStationId())
                .like(ObjectUtil.isNotEmpty(param.getGatewayId()), GatewayFirmware::getGatewayIds, "[" + param.getGatewayId() + "]")
                .orderByDesc(GatewayFirmware::getCreateTime);

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<GatewayFirmware> list(GatewayFirmwareParam gatewayFirmwareParam) {

        // 构造条件
        LambdaQueryWrapper<GatewayFirmware> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(GatewayFirmwareParam param) {


        GatewayFirmware firmware = BeanUtil.toBean(param, GatewayFirmware.class);
        if (ObjectUtil.isNotEmpty(param.getGatewayIds())) {
            StringBuilder gatewayIdsBud = new StringBuilder();
            // 处理对应网关
            for (Integer id : param.getGatewayIds()) {
                gatewayIdsBud.append("[").append(id).append("],");
            }
            gatewayIdsBud.deleteCharAt(gatewayIdsBud.length() - 1);
            firmware.setGatewayIds(gatewayIdsBud.toString());
        }
        this.save(firmware);

        taskService.add(GatewayTaskTypeConstant.UPDATE_FIRMWARE, param.getGatewayIds(), JSONUtil.toJsonStr(firmware));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(GatewayFirmwareParam gatewayFirmwareParam) {
        this.removeById(gatewayFirmwareParam.getId());
    }

    @Override
    public void edit(GatewayFirmwareParam gatewayFirmwareParam) {

        // 根据id查询实体
        GatewayFirmware gatewayFirmware = this.queryGatewayFirmware(gatewayFirmwareParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(gatewayFirmwareParam, gatewayFirmware);

        this.updateById(gatewayFirmware);
    }

    @Override
    public GatewayFirmware detail(GatewayFirmwareParam gatewayFirmwareParam) {
        return this.queryGatewayFirmware(gatewayFirmwareParam);
    }

    /**
     * 获取网关固件表
     *
     * @author zxw
     * @since 2022/11/03 11:41
     */
    private GatewayFirmware queryGatewayFirmware(GatewayFirmwareParam gatewayFirmwareParam) {
        GatewayFirmware gatewayFirmware = this.getById(gatewayFirmwareParam.getId());
        if (ObjectUtil.isEmpty(gatewayFirmware)) {
            throw new ServiceException(GatewayFirmwareExceptionEnum.NOT_EXISTED);
        }
        return gatewayFirmware;
    }

}
